home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / alv.sun / alv.lha / src / array2ras.c next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  2.5 KB  |  98 lines

  1. #include <stdio.h>
  2. #include "defs.h"
  3.  
  4. har   *progname;
  5. har   *filename;
  6. ixrect *pr;
  7.  
  8. #ifdef STANDALONE
  9. ain(argc, argv, envp)
  10. #else
  11. rray2ras_main(argc, argv, envp)
  12. #endif
  13.     int     argc;
  14.     char  **argv;
  15.     char  **envp;
  16. {
  17.     colormap_t colormap;
  18.     register int x, y;
  19.     int     xsize, ysize, depth;
  20.     short  *shortimage, *shortptr;
  21.     unsigned char *charimage, *charptr;
  22.     long   *longimage, *longptr;
  23.  
  24.     xsize = ysize = 512;
  25.     depth = 8;
  26.     progname = strsave(argv[0]);
  27.     parse_profile(&argc, argv, envp);
  28.  
  29.     while ((gc = getopt(argc, argv, "X:Y:d:")) != EOF)
  30.         switch (gc) {
  31.         case 'X':
  32.             xsize = atoi(optarg);
  33.             break;
  34.         case 'Y':
  35.             ysize = atoi(optarg);
  36.             break;
  37.         case 'd':
  38.             depth = atoi(optarg);
  39.             break;
  40.         case '?':
  41.             errflag++;
  42.             break;
  43.         }
  44.  
  45.     if (errflag)
  46.         error((char *) 0, "Usage: %s: [-X xsize] [-Y ysize] [-d depth] [infile] [outfile]\n", progname);
  47.  
  48.     for (stream = 0; optind < argc; stream++, optind++)
  49.         if (stream < 2 && strcmp(argv[optind], "-") != 0)
  50.             if (freopen(argv[optind], mode[stream], f[stream]) == NULL)
  51.                 error("%s %s", PR_IO_ERR_INFILE, argv[optind]);
  52.  
  53.     if (depth != 8 && depth != 16 && depth != 32)
  54.         error("Cannot read images of that depth");
  55.  
  56.     if ((pr = mem_create(xsize, ysize, depth)) == NULL)
  57.         error("mem_create returned NULL");
  58.  
  59.     switch (depth) {
  60.     case 8:
  61.         charimage = (unsigned char *) malloc(sizeof(unsigned char) * pr->pr_size.x);
  62.         for (y = 0; y < pr->pr_size.y; y++) {
  63.             charptr = charimage;
  64.             if (fread(charimage, sizeof(*charimage), pr->pr_size.x, stdin) != pr->pr_size.x)
  65.                 error("Error reading image data (possible file size error)");
  66.             for (x = 0; x < pr->pr_size.x; x++)
  67.                 pr_put(pr, x, y, (char) *charptr++);
  68.         }
  69.         free(charimage);
  70.         break;
  71.     case 16:
  72.         shortimage = (short *) malloc(sizeof(short) * pr->pr_size.x);
  73.         for (y = 0; y < pr->pr_size.y; y++) {
  74.             shortptr = shortimage;
  75.             if (fread(shortimage, sizeof(*shortimage), pr->pr_size.x, stdin) != pr->pr_size.x)
  76.                 error("Error reading image data (possible file size error)");
  77.             for (x = 0; x < pr->pr_size.x; x++)
  78.                 pr_put(pr, x, y, (short) *shortptr++);
  79.         }
  80.         free(shortimage);
  81.         break;
  82.     case 32:
  83.         longimage = (long *) malloc(sizeof(long) * pr->pr_size.x);
  84.         for (y = 0; y < pr->pr_size.y; y++) {
  85.             longptr = longimage;
  86.             if (fread(longimage, sizeof(*longimage), pr->pr_size.x, stdin) != pr->pr_size.x)
  87.                 error("Error reading image data (possible file size error)");
  88.             for (x = 0; x < pr->pr_size.x; x++)
  89.                 pr_put(pr, x, y, (long) *longptr++);
  90.         }
  91.         free(longimage);
  92.         break;
  93.     }
  94.     colormap.type = RMT_NONE;
  95.     colormap.length = 0;
  96.     pr_dump(pr, stdout, &colormap, RT_STANDARD, 0);
  97. }
  98.